﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class Users : YJ.Data.Interface.IUsers
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public Users()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.Users实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.Users model)
        {
            string sql = @"INSERT INTO users
				(ID,Name,Account,Password,Status,Sort,Note,Mobile,Tel,OtherTel,Fax,Email,QQ,HeadImg,WeiXin,Sex) 
				VALUES(@ID,@Name,@Account,@Password,@Status,@Sort,@Note,@Mobile,@Tel,@OtherTel,@Fax,@Email,@QQ,@HeadImg,@WeiXin,@Sex)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@Name", MySqlDbType.VarChar, 50){ Value = model.Name },
				new MySqlParameter("@Account", MySqlDbType.VarChar, 255){ Value = model.Account },
				new MySqlParameter("@Password", MySqlDbType.Text, -1){ Value = model.Password },
				new MySqlParameter("@Status", MySqlDbType.Int32, 11){ Value = model.Status },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = model.Note },
                model.Mobile == null ? new MySqlParameter("@Mobile", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Mobile", MySqlDbType.VarChar, 50) { Value = model.Mobile },
				model.Tel == null ? new MySqlParameter("@Tel", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@Tel", MySqlDbType.VarChar, 500) { Value = model.Tel },
				model.OtherTel == null ? new MySqlParameter("@OtherTel", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@OtherTel", MySqlDbType.VarChar, 500) { Value = model.OtherTel },
				model.Fax == null ? new MySqlParameter("@Fax", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Fax", MySqlDbType.VarChar, 50) { Value = model.Fax },
				model.Email == null ? new MySqlParameter("@Email", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@Email", MySqlDbType.VarChar, 500) { Value = model.Email },
				model.QQ == null ? new MySqlParameter("@QQ", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@QQ", MySqlDbType.VarChar, 50) { Value = model.QQ },
				model.HeadImg == null ? new MySqlParameter("@HeadImg", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@HeadImg", MySqlDbType.VarChar, 500) { Value = model.HeadImg },
				model.WeiXin == null ? new MySqlParameter("@WeiXin", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@WeiXin", MySqlDbType.VarChar, 50) { Value = model.WeiXin },
                !model.Sex.HasValue ? new MySqlParameter("@Sex", MySqlDbType.Int32, 11) { Value = DBNull.Value } : new MySqlParameter("@Sex", MySqlDbType.Int32, 11) { Value = model.Sex.Value }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.Users实体类</param>
        public int Update(YJ.Data.Model.Users model)
        {
            string sql = @"UPDATE Users SET 
				Name=@Name,Account=@Account,Password=@Password,Status=@Status,Sort=@Sort,Note=@Note,Mobile=@Mobile,Tel=@Tel,OtherTel=@OtherTel,Fax=@Fax,Email=@Email,QQ=@QQ,HeadImg=@HeadImg,WeiXin=@WeiXin,Sex=@Sex
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@Name", MySqlDbType.VarChar, 50){ Value = model.Name },
				new MySqlParameter("@Account", MySqlDbType.VarChar, 255){ Value = model.Account },
				new MySqlParameter("@Password", MySqlDbType.Text, -1){ Value = model.Password },
				new MySqlParameter("@Status", MySqlDbType.Int32, 11){ Value = model.Status },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.LongText, -1) { Value = model.Note },
                model.Mobile == null ? new MySqlParameter("@Mobile", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Mobile", MySqlDbType.VarChar, 50) { Value = model.Mobile },
				model.Tel == null ? new MySqlParameter("@Tel", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@Tel", MySqlDbType.VarChar, 500) { Value = model.Tel },
				model.OtherTel == null ? new MySqlParameter("@OtherTel", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@OtherTel", MySqlDbType.VarChar, 500) { Value = model.OtherTel },
				model.Fax == null ? new MySqlParameter("@Fax", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@Fax", MySqlDbType.VarChar, 50) { Value = model.Fax },
				model.Email == null ? new MySqlParameter("@Email", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@Email", MySqlDbType.VarChar, 500) { Value = model.Email },
				model.QQ == null ? new MySqlParameter("@QQ", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@QQ", MySqlDbType.VarChar, 50) { Value = model.QQ },
				model.HeadImg == null ? new MySqlParameter("@HeadImg", MySqlDbType.VarChar, 500) { Value = DBNull.Value } : new MySqlParameter("@HeadImg", MySqlDbType.VarChar, 500) { Value = model.HeadImg },
				model.WeiXin == null ? new MySqlParameter("@WeiXin", MySqlDbType.VarChar, 50) { Value = DBNull.Value } : new MySqlParameter("@WeiXin", MySqlDbType.VarChar, 50) { Value = model.WeiXin },
                !model.Sex.HasValue ? new MySqlParameter("@Sex", MySqlDbType.Int32, 11) { Value = DBNull.Value } : new MySqlParameter("@Sex", MySqlDbType.Int32, 11) { Value = model.Sex.Value },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM users WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.Users> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.Users> List = new List<YJ.Data.Model.Users>();
            YJ.Data.Model.Users model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.Users();
                model.ID = dataReader.GetString(0).ToGuid();
                model.Name = dataReader.GetString(1);
                model.Account = dataReader.GetString(2);
                model.Password = dataReader.GetString(3);
                model.Status = dataReader.GetInt32(4);
                model.Sort = dataReader.GetInt32(5);
                if (!dataReader.IsDBNull(6))
                    model.Note = dataReader.GetString(6);
                if (!dataReader.IsDBNull(7))
                    model.Mobile = dataReader.GetString(7);
                if (!dataReader.IsDBNull(8))
                    model.Tel = dataReader.GetString(8);
                if (!dataReader.IsDBNull(9))
                    model.OtherTel = dataReader.GetString(9);
                if (!dataReader.IsDBNull(10))
                    model.Fax = dataReader.GetString(10);
                if (!dataReader.IsDBNull(11))
                    model.Email = dataReader.GetString(11);
                if (!dataReader.IsDBNull(12))
                    model.QQ = dataReader.GetString(12);
                if (!dataReader.IsDBNull(13))
                    model.HeadImg = dataReader.GetString(13);
                if (!dataReader.IsDBNull(14))
                    model.WeiXin = dataReader.GetString(14);
                if (!dataReader.IsDBNull(15))
                    model.Sex = dataReader.GetInt32(15);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.Users> GetAll()
        {
            string sql = "SELECT * FROM users";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.Users> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM users";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.Users Get(Guid id)
        {
            string sql = "SELECT * FROM users WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.Users> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 根据帐号查询一条记录
        /// </summary>
        public YJ.Data.Model.Users GetByAccount(string account)
        {
            string sql = "SELECT * FROM Users WHERE Account=@Account";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@Account", MySqlDbType.VarChar, 255){ Value = account }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.Users> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 查询一个岗位下所有人员
        /// </summary>
        /// <param name="organizeID"></param>
        /// <returns></returns>
        public List<Model.Users> GetAllByOrganizeID(Guid organizeID)
        {
            string sql = "SELECT * FROM Users WHERE ID in(SELECT UserID FROM UsersRelation WHERE OrganizeID=@OrganizeID) ORDER BY Sort";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@OrganizeID", MySqlDbType.VarChar){ Value = organizeID.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.Users> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询一组岗位下所有人员
        /// </summary>
        /// <param name="organizeID"></param>
        /// <returns></returns>
        public List<Model.Users> GetAllByOrganizeIDArray(Guid[] organizeIDArray)
        {
            if (organizeIDArray == null || organizeIDArray.Length == 0)
            {
                return new List<Model.Users>();
            }
            string sql = "SELECT * FROM Users WHERE ID in(SELECT UserID FROM UsersRelation WHERE OrganizeID in(" + YJ.Utility.Tools.GetSqlInString(organizeIDArray) + ")) ORDER BY Sort";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.Users> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 检查帐号是否重复
        /// </summary>
        /// <param name="account">帐号</param>
        /// <param name="userID">人员ID(此人员除外)</param>
        /// <returns></returns>
        public bool HasAccount(string account, string userID = "")
        {
            string sql = "SELECT ID FROM Users WHERE Account=@Account";
            List<MySqlParameter> plist = new List<MySqlParameter>();
            plist.Add(new MySqlParameter("@Account", MySqlDbType.VarChar) { Value = account });
            if (userID.IsGuid())
            {
                sql += " and ID<>@ID";
                plist.Add(new MySqlParameter("@ID", MySqlDbType.VarChar) { Value = userID });
            }
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, plist.ToArray());
            bool flag = dataReader.HasRows;
            dataReader.Close();
            return flag;
        }
        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool UpdatePassword(string password, Guid userID)
        {
            string sql = "UPDATE Users SET Password=@Password WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@Password", MySqlDbType.VarChar){ Value = password },
                new MySqlParameter("@ID", MySqlDbType.VarChar){ Value = userID.ToString() }
			};
            return dbHelper.Execute(sql, parameters) == 1;
        }

        /// <summary>
        /// 更新排序
        /// </summary>
        public int UpdateSort(Guid userID, int sort)
        {
            string sql = "UPDATE Users SET Sort=@Sort WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@Sort", MySqlDbType.Int32){ Value = sort },
                new MySqlParameter("@ID", MySqlDbType.VarChar){ Value = userID.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }


        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.Users> GetAllByIDString(string idString)
        {
            string sql = "SELECT * FROM Users WHERE ID IN(" + Utility.Tools.GetSqlInString(idString) + ")";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.Users> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 查询一个角色组下的所有人员
        /// </summary>
        /// <param name="workgroupid"></param>
        /// <returns></returns>
        public List<YJ.Data.Model.Users> GetAllByWorkGroupID(Guid workgroupid)
        {
            string sql = "SELECT * FROM Users WHERE ID IN(SELECT UserID FROM WorkGroupUsers WHERE WorkGroupID='" + workgroupid + "')";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.Users> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}